import req from '/utils/Request.js';
import Wx from '/utils/Wx.js';
import appReq from '/utils/AppRequest.js';
App({
  configReq: function () {
    let url = "https://fang.weilianhuzhu.cn";
    req.baseUrl(url).interceptor(res => {
      //处理res的状态
      return true;
    });
  },
  onLaunch: function (options) {
    if (options.shareCustomerId) {
      this.setData({
        shareCustomerId: options.shareCustomerId,
      })
    }

    this.configReq(); 

    // 获取用户信息
    wx.getSetting({
      success: res => {
        console.log(res)
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              console.log(res)
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            },
            fail:function(res) {
              console.log(res)
            }
          })
        }
      }
    }) 

    Wx.login().then(res => {
      let code = res.code;
      if (code) {
        req.postFormData('/customer/login.do', { code: code, shareCustomerId: this.globalData.shareCustomerId }).then(res => {
          this.globalData.openId = res.data.openId;
          this.globalData.sessionKey = res.data.sessionKey;
          if (this.openIdReadyCallback) {
            this.openIdReadyCallback(res)
          }
        });
        
      }else{
        console.log('登录失败！' + res.errMsg);
      }
      
    }).catch(res => {
      wx.hideLoading();
      console.log(res);
    }); 


  },

  onShow: function (value) {
    if (value.shareCustomerId) {
      this.setData({
        shareCustomerId: value.shareCustomerId,
      })
    }
  },

  globalData: {
    openId: null,
    mobile: null,
    userInfo: null,
    shareCustomerId: 0,
    sessionKey: null
  },
})